package com.ruoyi.file.utils;

import com.ruoyi.file.pdfFileHandle.domain.DateInfoVO;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName StringRegexUtil
 * @Author fanzf
 * @Description
 * @Date 2024/9/30 10:33
 */
public class StringRegexUtil {
    /**
     * 通过正则获取字符位置
     * @param regex
     * @param source
     * @return
     */
    public static int charLocationByRegex(String regex, String source) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);
        int end = 0;
        if (matcher.find()) {
            end = matcher.start();
        }
        return end;
    }

    /**
     * 获取字符串中的 年-月-日 时-分-秒
     * @param dateSource
     * @return
     */
    public static DateInfoVO yearMonthDayTime(String dateSource) {
        Pattern pattern = Pattern.compile("(\\d{4})年(\\d{2})月(\\d{2})日\\s+(\\d{2}):(\\d{2}):(\\d{2})");
        Matcher matcher = pattern.matcher(dateSource);
        DateInfoVO dateInfoVO = new DateInfoVO();
        if (matcher.find()) {
            String year = matcher.group(1);
            dateInfoVO.setYear(year);
            String month = matcher.group(2);
            dateInfoVO.setMonth(month);
            String day = matcher.group(3);
            dateInfoVO.setDay(day);
            String hour = matcher.group(4);
            dateInfoVO.setHour(hour);
            String minute = matcher.group(5);
            dateInfoVO.setMinute(minute);
            String second = matcher.group(6);
            dateInfoVO.setSecond(second);
        }
        return dateInfoVO;
    }

    /**
     * 获取正则匹配的字符
     * @param regex
     * @param source
     * @return
     */
    public static String matcherString(String regex, String source) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);
        String target = null;
        if (matcher.find()) {
            target = matcher.group();
        }
        return target;
    }
}
